const app = getApp()
const customerRequest = (url, data, method = 'GET') => {

 
  //data['token'] = 'Bearer ' + app.globalData.token;
  return new Promise((res, rej) => {
    wx.request({
      url: app.globalData.apiCustomerUrl + url,
      method: method,
      data: data,
      header: { 'content-type': 'application/json', 'Authorization': 'Bearer ' + app.globalData.token },
      success(obj) {
        if (obj.data.message === 'Unauthenticated.') {
          wx.redirectTo({
            url: '/pages/login/index',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
        if (obj.statusCode >= 200 && obj.statusCode < 300 || obj.statusCode === 304) {
          if(obj.data.code!=200){
            wx.showToast({
              title: obj.data.res,
             icon:'none'
        
      
            })
            rej({
              msg: `网络错误:${obj.statusCode}`,
              detail: obj
            });
          }else{
            res(obj.data);
          }
        
        }
        else {
          rej({
            msg: `网络错误:${obj.statusCode}`,
            detail: obj
          });
        }
      },
      fail(err) {
        rej(err);
      }
    })
  })
}
export default customerRequest